
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;

public class CheckAssetsUpdate : MonoBehaviour
{
    
    private void Start() {
        StartCoroutine(CheckUpdate());
        /*
        void LoadTextAssets_Completed( AsyncOperationHandle<TextAsset> obj ){
            if (obj.Status != AsyncOperationStatus.Succeeded) return;
            Assembly hotUpdateAss = Assembly.Load(obj.Result.bytes);
            Type type = hotUpdateAss.GetType("RockFindTarget");
            //type.GetMethod("Start").Invoke(null, null);
        }
        Addressables.LoadAssetAsync<TextAsset>("HotUpdate.dll.bytes").Completed += LoadTextAssets_Completed;



        void LoadGameObjectAssets_Completed( AsyncOperationHandle<GameObject> obj ){
            if (obj.Status != AsyncOperationStatus.Succeeded) return;
            var o = GameObject.Instantiate(obj.Result);
        }


        Addressables.LoadAssetAsync<GameObject>("RockGurouce").Completed += LoadGameObjectAssets_Completed;
        */


    }
    private IEnumerator CheckUpdate(){
        AsyncOperationHandle<long> downloadSizeOpHandle = Addressables.GetDownloadSizeAsync("all");
        yield return downloadSizeOpHandle;
        if(downloadSizeOpHandle.Status == AsyncOperationStatus.Succeeded){
            //Debug.Log("Download Size : " + downloadSizeOpHandle.Result);
            if (downloadSizeOpHandle.Result > 0){
                Debug.Log("Exists Update");
                EnterGame();
            }else{
                Debug.Log("UptoDate");
                StartCoroutine(DownloadUpdates());
            }
        }

    }

    private IEnumerator DownloadUpdates(){
        AsyncOperationHandle  downloadOpHandle = Addressables.DownloadDependenciesAsync("all");
        while(! downloadOpHandle.IsDone){
            var downloadBytes = downloadOpHandle.GetDownloadStatus().DownloadedBytes;
            var totalBytes = downloadOpHandle.GetDownloadStatus().TotalBytes;
            Debug.Log("Download Progress : " + downloadBytes + "/" + totalBytes);
            yield return null;
        }
        if (downloadOpHandle.Status == AsyncOperationStatus.Succeeded){
            Addressables.Release(downloadOpHandle);
            EnterGame();
        }
    }

    private  async void EnterGame(){

        var loadDllAsync = Addressables.LoadAssetAsync<TextAsset>("HotUpdate.dll");
        await loadDllAsync.Task;
        Assembly hotUpdateAss = Assembly.Load(loadDllAsync.Result.bytes);
        Type T =  hotUpdateAss.GetType("HotUpdateInit");
        AsyncOperationHandle<SceneInstance> sceneLoader = Addressables.LoadSceneAsync("Main", LoadSceneMode.Single);
        sceneLoader.Completed += (AsyncOperationHandle<SceneInstance> op)=>{
            if (op.Status == AsyncOperationStatus.Succeeded){
                T.GetMethod("Init").Invoke(null, null);
            }
        };

    }

}
